<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sede extends CI_Controller{

	function __construct(){
		parent::__construct();
		$this->load->model('Mantenedores_model');
		$this->load->model('User_model');
		$this->_is_logued_in();
		$this->load->helper("form");
		$this->load->library("form_validation");
		$this->load->library("email");
		$this->load->library("fpdf");
		$this->load->library('table');
		$this->load->library('pagination');
	}
	
	private $limit = 4;
	
	function index($offset = 0, $order_column = 'cod_sede', $order_type = 'asc'){
		// checks
		if (empty($offset)) $offset = 0;
		if (empty($order_column)) $order_column = 'cod_sede';
		if (empty($order_type)) $order_type = 'asc';
			
		$sedes = $this->Mantenedores_model->get_paged_list($this->limit, $offset, $order_column, $order_type)->result();
	 
		// generate pagination
		
		$config['base_url'] = ''. base_url().'sede/index/';
		$config['total_rows'] = $this->Mantenedores_model->count_all();
		$config['per_page'] = $this->limit;
		$config['uri_segment'] = 3;
		$config['first_link'] = 'Primero';
		$config['last_link'] = 'Ultimo';
		$config['num_links'] = 3;

		$this->pagination->initialize($config);
		$data['pagination'] = $this->pagination->create_links();

		// generate table data
		$this->table->set_empty("&nbsp;");
		$new_order = ($order_type == 'asc' ? 'desc' : 'asc');
		$this->table->set_heading(
			'No ',
			anchor('sede/index/'.$offset.'/cod_sede/'.$new_order, 'ID'),
			anchor('sede/index/'.$offset.'/descripcion/'.$new_order, 'Nombre'),
			anchor('sede/index/'.$offset.'/dpto/'.$new_order, 'Departamento'),
		/*	anchor('noticias/index/'.$offset.'/mostrar/'.$new_order, 'Mostrar'),
			anchor('noticias/index/'.$offset.'/principal/'.$new_order, 'Principal'), */
			' Actions'
		);
		$i = 0 + $offset;
		foreach ($sedes as $sede){
			$this->table->add_row(++$i, $sede->cod_sede, $sede->descripcion,$sede->dpto,
				anchor('sede/view/'.$sede->cod_sede,'Ver',array('class'=>'view')).' '.
				anchor('sede/updates/'.$sede->cod_sede,'editar',array('class'=>'updates')).' '.
				anchor('sede/delete/'.$sede->cod_sede,'eliminar',array('class'=>'delete','onclick'=>"return confirm('Está seguro que desea eliminar esta Sede?')"))
			);
		}
		$data['title']= 'Cipost :: Sedes';
		$data['activo'] = "administrar";
		$data['table'] = $this->table->generate();
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['main_content']="mantenedores/sede";
		$this->load->view('includes/template',$data);
	}
	
	function view($id){
		$data['title'] = 'CIPOST :: Detalle de Sedes';
		$data['activo'] = "administrar";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['link_back'] = anchor('sede/index/','Volver a Sedes',array('class'=>'back'));
		// get person details
		$data['sede']=$this->Mantenedores_model->get_by_id($id)->row();
		$data['main_content']="mantenedores/sedeview";
		$this->load->view('includes/template',$data);

	}
	
	function add(){
		$data['title'] = 'CIPOST :: Agregar Sede';
		$data['activo'] = "administrar";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['message'] = '';
		$data['action'] = site_url('sede/addSede');
		$data['link_back'] = anchor('sede/index/','Volver a Sedes',array('class'=>'back'));
		$data['main_content']="mantenedores/sedeEdit";
		$this->load->view('includes/template',$data);
	}
	
	function addSede(){
		// set common properties
		$data['title'] = 'CIPOST :: Agregar Sede';
		$data['activo'] = "administrar";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['action'] = site_url('sede/addSede');
		$data['link_back'] = anchor('sede/index/','Volver a Sedes',array('class'=>'back'));

		$sedes = array('descripcion' => $this->input->post('descripcion'),
							'dpto' => $this->input->post('dpto'));
		$id = $this->Mantenedores_model->save($sedes);
		$data['message'] = '<div class="success">Se agreg&oacute; con &Eacute;xito</div>';	
		$data['main_content']="mantenedores/sedeEdit";
		$this->load->view('includes/template',$data);
	}
	
	function updates($id){
		$sede = $this->Mantenedores_model->get_by_id($id)->row();
		$data['sede']=$sede;
		$data['title']= 'CIPOST :: Actualizar Sede';
		$data['activo'] = "administrar";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['message'] = '';
		$data['action'] = site_url('sede/actualizarsede');
		$data['link_back'] = anchor('sede/index/','Volver a Sedes',array('class'=>'back'));
		$data['main_content']="mantenedores/sedeEdit";
		$this->load->view('includes/template',$data);
	}

	function actualizarsede(){
		// set common properties
		$data['title']= 'CIPOST :: Actualizar Sede';
		$data['activo'] = "administrar";
		$data['nombresession'] = $this->User_model->nombresession($this->session->userdata('id'))->row();
		$data['action'] = site_url('sede/actualizarsede');
		$data['link_back'] = anchor('sede/index/','Volver a Sedes',array('class'=>'back'));
	
		$cod_sede = $this->input->post('codigo');
		$sede = array('descripcion' => $this->input->post('descripcion'),
							'dpto' => $this->input->post('dpto'));
		$this->Mantenedores_model->actualiza($cod_sede,$sede);
		$data['message'] = '<div class="success">Sede Actualizada con &Eacute;xito!!!</div>';
		$data['main_content']="mantenedores/sedeEdit";
		$this->load->view('includes/template',$data);
	}
	

	function delete($id){
		$this->Mantenedores_model->delete($id);
		redirect('sede/index/','refresh');
	}
	
	function _is_logued_in(){
		$is_logued_in=$this->session->userdata('is_logued_in');
		$user_id=$this->session->userdata('id');
		if($is_logued_in!=TRUE or $user_id='')		
			redirect('/index.php/user/login');
	}
	
}

?>